{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[5, 3, 4]\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<table><tbody><tr><td style='text-align:center;vertical-align:middle; margin: 0.5em;border:1px #90999f solid;border-collapse:collapse'><img style='max-width: 100px; max-height:100px;display:inline' src=\"\"></td><td style='text-align:center;vertical-align:middle; margin: 0.5em;border:1px #90999f solid;border-collapse:collapse'><img style='max-width: 100px; max-height:100px;display:inline' src=\"\"></td><td style='text-align:center;vertical-align:middle; margin: 0.5em;border:1px #90999f solid;border-collapse:collapse'><img style='max-width: 100px; max-height:100px;display:inline' src=\"\"></td></tr></tbody></table><div><small>(a vector displayed as a row to save space)</small></div>"
      ],
      "text/plain": [
       "3-element Array{Base.ReinterpretArray{Gray{Float32},2,Float32,Array{Float32,2}},1}:\n",
       " [Gray{Float32}(0.0f0) Gray{Float32}(0.0f0) … Gray{Float32}(0.0f0) Gray{Float32}(0.0f0); Gray{Float32}(0.0f0) Gray{Float32}(0.0f0) … Gray{Float32}(0.0f0) Gray{Float32}(0.0f0); … ; Gray{Float32}(0.0f0) Gray{Float32}(0.0f0) … Gray{Float32}(0.0f0) Gray{Float32}(0.0f0); Gray{Float32}(0.0f0) Gray{Float32}(0.0f0) … Gray{Float32}(0.0f0) Gray{Float32}(0.0f0)]\n",
       " [Gray{Float32}(0.0f0) Gray{Float32}(0.0f0) … Gray{Float32}(0.0f0) Gray{Float32}(0.0f0); Gray{Float32}(0.0f0) Gray{Float32}(0.0f0) … Gray{Float32}(0.0f0) Gray{Float32}(0.0f0); … ; Gray{Float32}(0.0f0) Gray{Float32}(0.0f0) … Gray{Float32}(0.0f0) Gray{Float32}(0.0f0); Gray{Float32}(0.0f0) Gray{Float32}(0.0f0) … Gray{Float32}(0.0f0) Gray{Float32}(0.0f0)]\n",
       " [Gray{Float32}(0.0f0) Gray{Float32}(0.0f0) … Gray{Float32}(0.0f0) Gray{Float32}(0.0f0); Gray{Float32}(0.0f0) Gray{Float32}(0.0f0) … Gray{Float32}(0.0f0) Gray{Float32}(0.0f0); … ; Gray{Float32}(0.0f0) Gray{Float32}(0.0f0) … Gray{Float32}(0.0f0) Gray{Float32}(0.0f0); Gray{Float32}(0.0f0) Gray{Float32}(0.0f0) … Gray{Float32}(0.0f0) Gray{Float32}(0.0f0)]"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Take a look at some random images and labels in data\n",
    "using Knet,Images,Random,MLDatasets\n",
    "xtrn,ytrn = MNIST.traindata(Float32); ytrn[ytrn .== 0] .= 10\n",
    "xtst,ytst = MNIST.testdata(Float32);  ytst[ytst .== 0] .= 10\n",
    "rp = randperm(10000)\n",
    "println(ytst[rp[1:3]])\n",
    "[ MNIST.convert2image(xtst[:,:,rp[i]]) for i in 1:3 ]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Main.LeNet"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "include(\"lenet.jl\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "This example learns to classify hand-written digits from the \\href{http://yann.lecun.com/exdb/mnist}{MNIST} dataset.  There are 60000 training and 10000 test examples. Each input x consists of 784 pixels representing a 28x28 image.  The pixel values are normalized to [0,1]. Each output y is converted to a ten-dimensional one-hot vector (a vector that has a single non-zero component) indicating the correct class (0-9) for a given image.  10 is used to represent 0.\n",
       "\n",
       "You can run the demo using \\texttt{julia lenet.jl} at the command line or \\texttt{julia> LeNet.main()} at the Julia prompt.  Use \\texttt{julia lenet.jl --help} or \\texttt{julia> LeNet.main(\"--help\")} for a list of options.  The dataset will be automatically downloaded.  By default the \\href{http://yann.lecun.com/exdb/lenet}{LeNet} convolutional neural network model will be trained for 10 epochs.  The accuracy for the training and test sets will be printed at every epoch and optimized parameters will be returned.\n",
       "\n"
      ],
      "text/markdown": [
       "This example learns to classify hand-written digits from the [MNIST](http://yann.lecun.com/exdb/mnist) dataset.  There are 60000 training and 10000 test examples. Each input x consists of 784 pixels representing a 28x28 image.  The pixel values are normalized to [0,1]. Each output y is converted to a ten-dimensional one-hot vector (a vector that has a single non-zero component) indicating the correct class (0-9) for a given image.  10 is used to represent 0.\n",
       "\n",
       "You can run the demo using `julia lenet.jl` at the command line or `julia> LeNet.main()` at the Julia prompt.  Use `julia lenet.jl --help` or `julia> LeNet.main(\"--help\")` for a list of options.  The dataset will be automatically downloaded.  By default the [LeNet](http://yann.lecun.com/exdb/lenet) convolutional neural network model will be trained for 10 epochs.  The accuracy for the training and test sets will be printed at every epoch and optimized parameters will be returned.\n"
      ],
      "text/plain": [
       "  This example learns to classify hand-written digits from the MNIST (http://yann.lecun.com/exdb/mnist) dataset. There are 60000 training\n",
       "  and 10000 test examples. Each input x consists of 784 pixels representing a 28x28 image. The pixel values are normalized to [0,1]. Each\n",
       "  output y is converted to a ten-dimensional one-hot vector (a vector that has a single non-zero component) indicating the correct class\n",
       "  (0-9) for a given image. 10 is used to represent 0.\n",
       "\n",
       "  You can run the demo using \u001b[36mjulia lenet.jl\u001b[39m at the command line or \u001b[36mjulia> LeNet.main()\u001b[39m at the Julia prompt. Use \u001b[36mjulia lenet.jl --help\u001b[39m or\n",
       "  \u001b[36mjulia> LeNet.main(\"--help\")\u001b[39m for a list of options. The dataset will be automatically downloaded. By default the LeNet\n",
       "  (http://yann.lecun.com/exdb/lenet) convolutional neural network model will be trained for 10 epochs. The accuracy for the training and\n",
       "  test sets will be printed at every epoch and optimized parameters will be returned."
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "@doc LeNet"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "usage: <PROGRAM> [--seed SEED] [--batchsize BATCHSIZE] [--lr LR]\n",
      "                 [--fast] [--epochs EPOCHS] [--iters ITERS]\n",
      "                 [--gcheck GCHECK] [--atype ATYPE]\n",
      "\n",
      "lenet.jl (c) Deniz Yuret, 2016. The LeNet model on the MNIST\n",
      "handwritten digit recognition problem from\n",
      "http://yann.lecun.com/exdb/mnist.\n",
      "\n",
      "optional arguments:\n",
      "  --seed SEED           random number seed: use a nonnegative int for\n",
      "                        repeatable results (type: Int64, default: -1)\n",
      "  --batchsize BATCHSIZE\n",
      "                        minibatch size (type: Int64, default: 128)\n",
      "  --lr LR               learning rate (type: Float64, default: 0.1)\n",
      "  --fast                skip loss printing for faster run\n",
      "  --epochs EPOCHS       number of epochs for training (type: Int64,\n",
      "                        default: 3)\n",
      "  --iters ITERS         maximum number of updates for training (type:\n",
      "                        Int64, default: 9223372036854775807)\n",
      "  --gcheck GCHECK       check N random gradients per parameter (type:\n",
      "                        Int64, default: 0)\n",
      "  --atype ATYPE         array and float type to use (default:\n",
      "                        \"KnetArray{Float32,N} where N\")\n",
      "\n"
     ]
    }
   ],
   "source": [
    "LeNet.main(\"--help\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "lenet.jl (c) Deniz Yuret, 2016. The LeNet model on the MNIST handwritten digit recognition problem from http://yann.lecun.com/exdb/mnist.\n",
      "opts=(:batchsize, 128)(:fast, false)(:iters, 9223372036854775807)(:atype, \"KnetArray{Float32,N} where N\")(:epochs, 3)(:gcheck, 0)(:lr, 0.1)(:seed, -1)\n",
      "(:epoch, 0, :trn, 0.10505141559829059, :tst, 0.10036057692307693)\n",
      " 17.581164 seconds (37.66 M allocations: 2.066 GiB, 2.47% gc time)\n",
      "(:epoch, 1, :trn, 0.9657952724358975, :tst, 0.9693509615384616)\n",
      "  1.015904 seconds (1.45 M allocations: 254.688 MiB, 3.59% gc time)\n",
      "(:epoch, 2, :trn, 0.9759949252136753, :tst, 0.9754607371794872)\n",
      "  1.008728 seconds (1.45 M allocations: 254.741 MiB, 3.22% gc time)\n",
      "(:epoch, 3, :trn, 0.9815872061965812, :tst, 0.9793669871794872)\n"
     ]
    }
   ],
   "source": [
    "model = LeNet.main(\"\");"
   ]
  }
 ],
 "metadata": {
  "@webio": {
   "lastCommId": null,
   "lastKernelId": null
  },
  "kernelspec": {
   "display_name": "Julia 1.5.3",
   "language": "julia",
   "name": "julia-1.5"
  },
  "language_info": {
   "file_extension": ".jl",
   "mimetype": "application/julia",
   "name": "julia",
   "version": "1.5.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
